﻿using Hang.Blog.DomainPlus.EntityframeworkExtension;
using Hang.Blog.Entities;

using Microsoft.EntityFrameworkCore;

using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace Hang.Blog.EntityFrameworkCore {
    public static class BlogDbContextModelCreatingExtensions {
        public static void ConfigureBlog(this ModelBuilder builder) {
            Check.NotNull(builder, nameof(builder));

            /* Configure your own tables/entities inside here */

            builder.Entity<Menu>(b => {
                b.ToTable(BlogConsts.DbTablePrefix + nameof(Menu), BlogConsts.DbSchema);
                b.ConfigureFullAuditedAggregateRootPlus(); //auto configure for the base class props
                b.Property(x => x.MenuName).HasMaxLength(200);
            });

            builder.Entity<Article>(b => {
                b.ToTable(BlogConsts.DbTablePrefix + nameof(Article), BlogConsts.DbSchema);
                b.ConfigureFullAuditedAggregateRootPlus(); //auto configure for the base class props

            });
            builder.Entity<ArticleAndTag>(b => {
                b.ToTable(BlogConsts.DbTablePrefix + nameof(ArticleAndTag), BlogConsts.DbSchema);
                b.ConfigureByConvention();
                b.HasKey(x => new { x.TagId, x.ArticleId });

            });
            builder.Entity<ArticleTag>(b => {
                b.ToTable(BlogConsts.DbTablePrefix + nameof(ArticleTag), BlogConsts.DbSchema);
                b.Property(x => x.TagName).HasMaxLength(400);
                b.ConfigureAuditedAggregateRoot();
            });

            builder.Entity<Category>(b => {
                b.ToTable(BlogConsts.DbTablePrefix + nameof(Category), BlogConsts.DbSchema);
                b.ConfigureAuditedAggregateRoot();

            });

        }
    }
}